package com.example.administrator.im.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.im.R;
import com.example.administrator.im.data.DetailsData;
import com.example.administrator.im.interfacz.Detailsable;

import java.util.List;

/**
 * create:2018/11/9 14:58
 * author: 姚鸿杰
 **/
public class DetailsAdapter extends RecyclerView.Adapter<DetailsAdapter.Holder> {

    private List<DetailsData> list;
    private Context context;

    public DetailsAdapter(List<DetailsData> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void updateItem(String content, int position) {

        if (list == null) return;
        DetailsData data = list.get(position);
        data.setContent(content);
        list.remove(position);
        list.add(position, data);
        notifyDataSetChanged();

    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(context).inflate(R.layout.recycler_info, viewGroup, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, final int i) {

        final DetailsData data = list.get(i);
        holder.title.setText(data.getTitle());
        holder.content.setText(data.getContent());

        if (data.isMarginTop()) {
            RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) holder.info.getLayoutParams();
            lp.topMargin = (int) context.getResources().getDimension(R.dimen.padding_size);
            holder.info.setLayoutParams(lp);
        }

        holder.info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Detailsable.onClick(v, i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Holder extends RecyclerView.ViewHolder {

        private LinearLayout info;
        private TextView title;
        private TextView content;

        public Holder(@NonNull View itemView) {
            super(itemView);

            info = (LinearLayout) itemView.findViewById(R.id.recycler_info);
            title = (TextView) itemView.findViewById(R.id.title);
            content = (TextView) itemView.findViewById(R.id.content);

        }
    }

    private Detailsable Detailsable;

    public void setOnClickListener(Detailsable Listener) {
        this.Detailsable = Listener;
    }

}
